<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateDutyLeadersTable extends Migration
{
    public function up()
    {
        Schema::create('duty_leaders', function (Blueprint $table) {
            $table->bigIncrements('id')->comment('ID');
            $table->date('duty_date')->comment('当班日期');
            $table->unsignedBigInteger('leader_id')->comment('班长ID');
            $table->string('leader_name')->comment('班长姓名');
            $table->string('team_name')->comment('班组名称');
            $table->tinyInteger('is_notified')->default(0)->comment('是否已发送微信提醒：0-否 1-是');
            $table->datetime('notified_at')->nullable()->comment('发送提醒时间');

            $table->timestamps();
            $table->softDeletes();

            $table->unique(['duty_date', 'team_name', 'deleted_at'], 'uk_duty_date_team')->comment('每天每个班组只能有一个当班班长');
            $table->index('duty_date', 'idx_duty_date')->comment('当班日期索引');
            $table->index('leader_id', 'idx_leader_id')->comment('班长ID索引');

            $table->foreign('leader_id', 'fk_duty_leader_leader')->references('id')->on('employees')->onDelete('cascade');
        });

        DB::statement('alter table' . ' ' . config('database.connections.mysql.prefix', '') . 'feedbacks comment "当班班长记录表"');

    }

    public function down()
    {
        Schema::dropIfExists('duty_leaders');
    }
}

